package com.lyg.app;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.AutoCompleteTextView;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;

import com.lyg.app.tencentx5.X5WebViewActivity;
import com.lyg.app.ui.HEWebViewActivity;
import com.lyg.app.utils.StatusBarUtil;
import com.lyg.hew.HEWebTools;


public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    // 是否开启了主页，没有开启则会返回主页
    public static boolean isLaunch = false;
    private AutoCompleteTextView etSearch;
    private RadioButton rbSystem;
    private int state = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        StatusBarUtil.setColor(this, ContextCompat.getColor(this, R.color.colorPrimary), 0);
        initView();
        isLaunch = true;
    }

    private void initView() {
        findViewById(R.id.bt_deeplink).setOnClickListener(this);
        findViewById(R.id.bt_openUrl).setOnClickListener(this);
        findViewById(R.id.bt_baidu).setOnClickListener(this);
        findViewById(R.id.bt_movie).setOnClickListener(this);
        findViewById(R.id.bt_upload_photo).setOnClickListener(this);
        findViewById(R.id.bt_call).setOnClickListener(this);
        findViewById(R.id.bt_java_js).setOnClickListener(this);
        findViewById(R.id.bt_toolbar).setOnClickListener(this);
        findViewById(R.id.bt_wvjbwebview).setOnClickListener(this);

        rbSystem = findViewById(R.id.rb_system);
        etSearch = findViewById(R.id.et_search);
        rbSystem.setChecked(true);
        TextView tvVersion = findViewById(R.id.tv_version);
        tvVersion.setText(String.format("❤版本：v%s", 1.0));
        tvVersion.setOnClickListener(this);
        /** 处理键盘搜索键 */
        etSearch.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    openUrl();
                }
                return false;
            }
        });
    }

    @Override
    public void onClick(View v) {
        if(v.getId() == R.id.bt_openUrl){
            openUrl();
        } else if (v.getId() == R.id.bt_baidu) {// 百度一下
            String baiDuUrl = "http://www.baidu.com";
            loadUrl(baiDuUrl, getString(R.string.text_baidu));
        }else if (v.getId() == R.id.bt_movie) { // 网络视频
            state = 0;
            String movieUrl = "https://www.bilibili.com/";
            loadUrl(movieUrl, getString(R.string.text_movie));
        } else if (v.getId() == R.id.bt_upload_photo) {
            state = 0;
            String uploadUrl = "file:///android_asset/upload_photo.html";
            loadUrl(uploadUrl, getString(R.string.text_upload_photo));
        }else if (v.getId() == R.id.bt_call){
            state = 1;
            String callUrl = "file:///android_asset/callsms.html";
            loadUrl(callUrl, getString(R.string.text_js));
        } else if ((v.getId() == R.id.bt_java_js)){
            state = 2;
            String javaJs = "file:///android_asset/java_js.html";
            loadUrl(javaJs, getString(R.string.js_android));
        }else if (v.getId() == R.id.bt_deeplink){
            state = 0;
            String deepLinkUrl = "file:///android_asset/deeplink.html";
            loadUrl(deepLinkUrl, getString(R.string.deeplink));
        } else if (v.getId() == R.id.tv_version) {
            AlertDialog.Builder builder = new AlertDialog.Builder(v.getContext());
            builder.setTitle("感谢");
            builder.setMessage("开源不易，给作者一个star好吗？😊");
            builder.setNegativeButton("已给", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    Toast.makeText(MainActivity.this, "感谢老铁~", Toast.LENGTH_LONG).show();
                }
            });
            builder.setPositiveButton("去star", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    state = 0;
                    loadUrl("https://gitee.com/liang-yonggang/horizon-explorer-webview", "Horizon Explorer WebView");
                }
            });
            builder.show();
        }
    }

    /**
     * 打开网页
     */
    private void openUrl() {
        state = 0;
        String url = HEWebTools.getUrl(etSearch.getText().toString().trim());
        loadUrl(!TextUtils.isEmpty(url) ? url : "https://gitee.com/liang-yonggang/horizon-explorer-webview", "Horizon Explorer WebView");
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    // TODO: 2025/9/8
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int itemId = item.getItemId();
        if(itemId == R.id.actionbar_update){  //更新选项
            state = 0;
            //todo 之后load一个安装包的下载链接
        } else if (itemId == R.id.actionbar_about) { //关于选项
            state = 0;
            loadUrl("https://gitee.com/liang-yonggang/horizon-explorer-webview", "Horizon Explorer WebView");
        }

        return super.onOptionsItemSelected(item);
    }

    private void loadUrl(String mUrl, String mTitle) {
        if (rbSystem.isChecked()) {
//            WebViewActivity.loadUrl(this, mUrl, mTitle);
            HEWebViewActivity.loadUrl(this, mUrl, mTitle, state);
        } else {
           X5WebViewActivity.loadUrl(this, mUrl, mTitle);
        }
    }

    public static void start(Context context) {
        context.startActivity(new Intent(context, MainActivity.class));
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        isLaunch = false;
    }
}

